package com.grj.parkingsystem.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.grj.parkingsystem.common.BusinessException;
import com.grj.parkingsystem.common.JwtUtil;
import com.grj.parkingsystem.entity.User;
import com.grj.parkingsystem.mapper.UserMapper;
import com.grj.parkingsystem.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Resource
    private JwtUtil jwtUtil;

    @Override
    public void register(String username, String password, String phone) {
        // 检查用户名是否已存在
        User existUser = userMapper.selectOne(
                new QueryWrapper<User>().eq("username", username)
        );
        
        if (existUser != null) {
            throw new BusinessException("用户名已存在");
        }

        // 创建新用户
        User user = new User();
        user.setUsername(username);
        user.setPassword(passwordEncoder.encode(password));
        user.setPhone(phone);
        user.setUserType(0);
        user.setRegisterTime(new Date());
        
        userMapper.insert(user);
    }

    @Override
    public Object login(String username, String password) {
        // 查询用户
        User user = userMapper.selectOne(
                new QueryWrapper<User>().eq("username", username)
        );
        
        if (user == null) {
            throw new BusinessException("用户名不存在");
        }

        // 验证密码
        if (!passwordEncoder.matches(password, user.getPassword())) {
            throw new BusinessException("密码错误");
        }

        // 生成JWT Token
        Map<String, Object> claims = new HashMap<>();
        claims.put("username", user.getUsername());
        claims.put("userType", user.getUserType());
        
        String token = jwtUtil.generateToken(claims);

        // 返回登录结果
        Map<String, Object> result = new HashMap<>();
        result.put("token", token);
        result.put("userInfo", user);
        
        return result;
    }

    @Override
    public User getUserInfo(String token) {
        // 解析Token
        String username = jwtUtil.getUsernameFromToken(token);
        // 查询用户信息
        User user = userMapper.selectOne(new QueryWrapper<User>().eq("username", username));
        user.setPassword(null);


        return user;
    }

    @Override
    public void updateUser(User user, String token) {
        // 解析Token获取用户名
        String username = jwtUtil.getUsernameFromToken(token);
        
        // 查询用户
        User existUser = userMapper.selectOne(
                new QueryWrapper<User>().eq("username", username)
        );
        
        if (existUser == null) {
            throw new BusinessException("用户不存在");
        }

        // 更新用户信息
        if (user.getPhone() != null) {
            existUser.setPhone(user.getPhone());
        }
        
        userMapper.updateById(existUser);
    }
}    